package com.csbaic.web.jackson;

 import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;

import java.io.IOException;
import java.time.LocalDateTime;
import java.time.ZoneId;

public class LocalDateTimeToLongSerializer extends StdSerializer<LocalDateTime> {

    public LocalDateTimeToLongSerializer( ) {
        super(LocalDateTime.class);
    }


    @Override
    public void serialize(LocalDateTime value, JsonGenerator gen, SerializerProvider provider) throws IOException {
        if (value != null) {
            gen.writeNumber(
                    value
                            .atZone(ZoneId.of(provider.getTimeZone().getID()))
                            .toInstant()
                            .toEpochMilli()
            );
            return;
        }

        provider.defaultSerializeValue(value, gen);
    }
}
